<template>
	<eui-page-frame topHeight="255">
		<template #top>
			<view class="content-img">
				<eui-navbar ref="EuiNavBar" title="单据列表" color="#000000" backgroundColor="#FFFFFF">
				</eui-navbar>
				<view class="top-view">
					<eui-search-bar ref="RdsSearchBar" v-model="searchText" placeholder="请输入单号" leftIcon="icon-saoyisao"
						@confirm="searchConfirm" @leftIconClick="scanCode" radius="30" showFilter="true">
						<template v-slot:searchFields>
							<view class="search-fields">
								<view class="center">
									<eui-card-input class="search-field" v-model="formData.c_mk_dt_s" title="开始日期"
										placeholder="请选择开始日期" type="date" :displayable="false" rightIconType="clear" @iconClick="clear" singleTag="c_mk_dt_s"/>
									<eui-card-input class="search-field" v-model="formData.c_mk_dt_e" title="结束日期"
										placeholder="请选择结束日期" type="date" :displayable="false" rightIconType="clear" @iconClick="clear" singleTag="c_mk_dt_e"/>
								</view>
								<view class="bottom flex-1 flex bt-button">
									<eui-button type="max2" title="重置" singleTag="reset"
										@buttonClick="confirm"></eui-button>
									<eui-button type="max" title="确认" singleTag="fix"
										@buttonClick="confirm"></eui-button>
								</view>
							</view>
						</template>
					</eui-search-bar>
				</view>
			</view>
		</template>
		<eui-page-content>
			<view class="footer-view">
				<scroll-view :scroll-top="0" scroll-y="true" style="height: auto;" @scrolltolower="loadMore">
					<eui-base-card v-for="(item, index) in dataList" :key="index" @click="templateClick(item)">
						<eui-detail-show :items="Items" v-model="dataList[index]" size="small"></eui-detail-show>
					</eui-base-card>
				</scroll-view>
				<!-- <eui-list-template  v-model="dataList" :title="false" refresher isImage @click="templateClick"
						@onRefresh='billList'>
						<template v-slot:index="scope">
							<eui-text v-model="scope.c_id" title="单号:" type="name"></eui-text>
							<eui-text v-model="scope.c_out_store_id" :name="getRelatedName('rdsStores', scope.c_out_store_id)"
								type="titlename" title="配送中心:" occupy style="grid-template-columns: 100%;"></eui-text>
							<eui-text v-model="scope.c_out_wno" :name="getRelatedName('rdsWnos', scope.c_out_wno)"
								type="titlename" title="配送仓库:" occupy style="grid-template-columns: 100%;"></eui-text>
							<eui-text v-model="scope.c_n_out" type="price" title="发货数量(含赠品):"></eui-text>
							<eui-text v-model="scope.c_at_move_out" type="price" title="配送金额:"></eui-text>
							
						</template>
						
				</eui-list-template> -->
			</view>
		</eui-page-content>
	</eui-page-frame>
</template>

<script setup type="ts">
	import { ref, reactive, onMounted } from 'vue';
	import {useMainStore} from "@/store/modules/store";
	import {useLoginStore} from "@/store/modules/useLoginStore";
	import { validateStores } from '@/hooks/useValidateMethod';
	import dateRangUtil from '../../../../common/dateRangUtil';
	import { getRelatedName, getDictName,getDictSelectList, getDictSelect } from '../../components/RelatedName.js';
	import Enjoy from "@/common/enjoy";
	import {onLoad} from "@dcloudio/uni-app";
	import { init } from '@/hooks/init';
	import useService from '@/hooks/useService';
	import dayjs from 'dayjs';
	const service = useService({ prefix: "scApi" });
	
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	const EuiNavBar = ref(null);
	const RdsSearchBar = ref(null);
	
	const searchText = ref('');
	const sUserStore = ref('');
	const sUserStoreName = ref('');
	const sUser = ref('');
	const displayable = ref(false);  //true允许页面编辑
	const formData = ref({
	   c_mk_dt_s: (dateRangUtil.getLast1Month())[0],
	   c_mk_dt_e: (dateRangUtil.getLast1Month())[2],
	})
	const dataList = ref([]);
	const reflush = ref(true);//是否刷新页面
	const current = ref('');
	const c_height = ref(0);
	const c_top = ref(0);
	const height = ref(0);
	const ISSearch = ref(false);
	const search = ref('');
	const c_id = ref(''); //流水号
	const c_status = ref('3'); //审核状态
	const c_d_status = ref('1');//发货状态
	// c_summary: '0,1,2',//收货状态
	const c_store_id = ref('');
	const c_gcode = ref('');
	const c_order_dt_s = ref(dayjs().subtract(7, 'days').format("YYYY-MM-DD"));
	const c_order_dt_e = ref(dayjs(new Date()).format("YYYY-MM-DD"));
	const CurrentPage = ref(1);  //第几页
	const PageSize = ref(10);  //每页10条
	const RowsCount = ref(0);  //总条数
	const PageCount = ref(1);  //总页数
	const { loginInfo } = mainStore;
	const Items = [
		{
			label: "单号",
			prop: "c_id",
			type: "text",
			colSpan: 24,
			element: {
			},
		},
		{
			label: "配送中心",
			prop: "c_out_store_name",
			type: "text",
			colSpan: 12,
			element: {
			},
		},
		{
			label: "配送仓库",
			prop: "c_out_wno_name",
			type: "text",
			colSpan: 12,
			element: {
			
			},
		},
		{
			label: "发货数量(含赠品)",
			prop: "c_n_out",
			type: "text",
			colSpan: 12,
			element: {
				precision: 0
			},
		},
		{
			label: "配送金额",
			prop: "c_at_move_out",
			type: "text",
			colSpan: 12,
			element: {
				precision: 2
			},
		}
	]
	
	onLoad(async (option) => {
	  c_store_id.value = option.storeId;
	  if (option.orderData) {
	  	dataList.value = JSON.parse(option.orderData);
	  	reflush.value = false;
	  } else {
	  	await billList();
	  }
	})
	const clear = (e)=>{
		switch (e) {
			case 'c_mk_dt_s':
			  formData.value.c_mk_dt_s = '';
			  break;
			case 'c_mk_dt_e':
			  formData.value.c_mk_dt_e = '';
			  break;
		}
	}
	const confirm = async(t) => {
	  let { singleTag } = t;
	  switch (singleTag) {
	    case 'reset':
	       formData.value.c_mk_dt_s = (dateRangUtil.getLast1Month())[0];
	       formData.value.c_mk_dt_e = (dateRangUtil.getLast1Month())[2];
	      break;
	    case 'fix':
	      // 关闭弹出层
	      RdsSearchBar.value.closeDrawer();
		  await billList();
	      break;
	  }
	}
	// 点击扫描图标跳转打开摄像头
	const scanCode = () => {
	  uni.scanCode({
	    success: function (res) {
	      if (res.result != '' && res.result != undefined && res.result != null) {
	        c_id.value = res.result;
	         billList();
	      }
	    },
	  });
	}
	const templateClick = (e) => {
		console.log('eeeeeeee',e)
		let pages = getCurrentPages(); //获取所有页面栈实例列表
		let prevPage = pages[pages.length - 2]; //上一页页面实例
		prevPage.$vm.$.exposed.SelectionChanged(e.c_id); //执行函数也是同理
		uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
			delta: 1
		});
	};
	const searchConfirm = async (e) => {
	  console.log('=========searchConfirm: ', e);
	  if (!e) {
	    return;
	  }
	  c_id.value = e.value;
	  await billList();
	}
	// 查询数据列表
	const billList = async(e) =>{
		if (!reflush.value) {
			return;
		}
		if (!e) {
			CurrentPage.value = 1;
			dataList.value = [];
		}
		uni.showLoading({ title: "正在加载...", mask: true })
		const req = {
			IsNotPage: false,
			CurrentPage: CurrentPage.value,
			PageSize: PageSize.value,
			UserNo: loginInfo.sUser,
			WhereList: [
				{ c_col: "c_id", c_oper: "左相似", c_value: c_id.value, c_logic: "而且" },
				c_gcode.value ? { c_col: "c_gcode", c_oper: "等于", c_value: c_gcode.value, c_logic: "而且" } : {},
				{ c_col: "c_in_store_id", c_oper: "等于", c_value: c_store_id.value, c_logic: "而且" },
				{ c_col: "c_status", c_oper: "等于", c_value: c_status.value, c_logic: "而且" },
				{ c_col: "c_d_status", c_oper: "等于", c_value: c_d_status.value, c_logic: "而且" },
			]
		}
		const result = await service.request('商品配送单', 'EnjoyQueryList', req,'801');
		uni.hideLoading();
		CurrentPage.value = result.CurrentPage; //第几页
		PageSize.value = result.PageSize; //每页10条
		RowsCount.value = result.RowsCount; //总条数
		PageCount.value = result.PageCount; //总页数 
		const _data =  result.ReturnObject.map(x=>{
				return {
					...x,
					c_out_store_name:x.c_out_store_id?'['+x.c_out_store_id+']'+getRelatedName('rdsStores', x.c_out_store_id):'',
					c_out_wno_name:x.c_out_wno?'['+x.c_out_wno+']'+getRelatedName('rdsWnos', x.c_out_wno):''
				}
			});
		if (result.CurrentPage == 1) {
			dataList.value = _data;
		} else {
			dataList.value = dataList.value.concat(_data);
		}
	}
	const loadMore = async() => {
		if (CurrentPage.value < PageCount.value) {
			CurrentPage.value = CurrentPage.value + 1;
			await billList(true);
		}
	}
</script>
<style lang="scss" scoped>
	@import '../../../../main.scss';
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #F2F2F2;
		padding: 0px !important
	}

	.content-img {
		background-color: #FFFFFF;
		background-size: 100% 176rpx;
		background-repeat: no-repeat;
		width: 100%;
	}

	.navigation {
		flex: 0 0 auto;
		margin-bottom: 5px;
	}
	.footer-view {
		width: 100%;
		flex: 1;
	}
	.bt-button {
		background-color: #FFFFFF;
		padding: 10rpx 0rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
</style>